#include "../../StickyInput.h"

void lineX(SDL_Surface* screen, int x1, int y, int x2, Uint32 color)
{
    if(x2 < x1)
    {
        int temp = x1;
        x1 = x2;
        x2 = temp;
    }
    
    SDL_Rect r = {x1, y, x2 - x1, 1};
    SDL_FillRect(screen, &r, color);
}

void lineY(SDL_Surface* screen, int y1, int x, int y2, Uint32 color)
{
    if(y2 < y1)
    {
        int temp = y1;
        y1 = y2;
        y2 = temp;
    }
    
    SDL_Rect r = {x, y1, 1, y2 - y1};
    SDL_FillRect(screen, &r, color);
}

int main(int argc, char* argv[])
{
    char buff[] = "SDL_VIDEO_CENTERED=center";
    SDL_putenv(buff);
    
    if( SDL_Init(SDL_INIT_VIDEO) < 0 )
    {
        printf("Couldn't initialize SDL: %s\n", SDL_GetError());
        return 0;
    }
    
    int w = 400, h = 400;

    SDL_Surface* screen = SDL_SetVideoMode(w, h, 0, SDL_SWSURFACE);

    if ( screen == NULL ) {
        printf("Couldn't set video mode %dx%d: %s\n", w, h, SDL_GetError());
		return 0;
    }
    printf(" running @ %dx%d %dbpp (done)\n", w, h, screen->format->BitsPerPixel);
    
    
    // Reset joysticks
    SI::Reset();
    
    if(SI::WasInit())
        printf("Ready to go!\n");
    else
    {
        printf("Failed initialization.");
        return 0;
    }
    
    for(int i = 0; i < SI::NumJoysticks(); i++)
    {
        SI::JoystickInfo info(i);
        printf("%s:\n  %d Axes\n  %d Buttons\n  %d Hats\n  %d Balls\n\n", info.name, info.axes, info.buttons, info.hats, info.balls);
    }
    
    printf("Use arrow keys.  Press ESCAPE to quit.\n\n");
    
    SI::Button buttons[4];// = {SI::Button(SDLK_UP), SI::Button(SDLK_DOWN), SI::Button(SDLK_LEFT), SI::Button(SDLK_RIGHT)};
    
    SDL_Event event;
    
    // UP
    SDL_FillRect(screen, NULL, 0x000000);
    lineY(screen, screen->w/2, screen->h/2, screen->h/2 - screen->h/2, SDL_MapRGB(screen->format, 0, 255, 0));
    SDL_Flip(screen);
    buttons[0] = SI::WaitPress(NULL, 8000);
    SDL_Delay(100);
    while(SDL_PollEvent(&event));
    
    // DOWN
    SDL_FillRect(screen, NULL, 0x000000);
    lineY(screen, screen->w/2, screen->h/2, screen->h/2 + screen->h/2, SDL_MapRGB(screen->format, 0, 255, 0));
    SDL_Flip(screen);
    buttons[1] = SI::WaitPress(NULL, 8000);
    SDL_Delay(100);
    while(SDL_PollEvent(&event));
    
    // LEFT
    SDL_FillRect(screen, NULL, 0x000000);
    lineX(screen, screen->w/2, screen->h/2, screen->w/2 - screen->w/2, SDL_MapRGB(screen->format, 0, 255, 0));
    SDL_Flip(screen);
    buttons[2] = SI::WaitPress(NULL, 8000);
    SDL_Delay(100);
    while(SDL_PollEvent(&event));
    
    // RIGHT
    SDL_FillRect(screen, NULL, 0x000000);
    lineX(screen, screen->w/2, screen->h/2, screen->w/2 + screen->w/2, SDL_MapRGB(screen->format, 0, 255, 0));
    SDL_Flip(screen);
    buttons[3] = SI::WaitPress(NULL, 8000);
    SDL_Delay(100);
    while(SDL_PollEvent(&event));
    
    for(int i = 0; i < 4; i++)
    {
        SI::PrintButton(buttons[i]);
    }
    
    bool circular = true;
    
    SI::DirectionSet dpad(buttons[0], buttons[1], buttons[2], buttons[3]);
    bool done = false;
    while(!done)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
                done = true;
            else if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
                done = true;
        }
        dpad.update(circular);
        if(dpad.state.magnitude > 1.01f)
            circular = false;
        
        SDL_FillRect(screen, NULL, 0x000000);
		
		if(dpad.state.magnitude > 0.01f)
		{
			printf("dpad: (%.2f, %.2f), %.2f\n", dpad.state.dirX, dpad.state.dirY, dpad.state.magnitude);
			lineX(screen, screen->w/2, screen->h/2, screen->w/2 + screen->w/2*dpad.state.dirX*dpad.state.magnitude, SDL_MapRGB(screen->format, 255, 0, 0));
			lineY(screen, screen->w/2, screen->h/2, screen->h/2 - screen->h/2*dpad.state.dirY*dpad.state.magnitude, SDL_MapRGB(screen->format, 255, 0, 0));
			
			SDL_Rect r = {screen->w/2 + screen->w/2*dpad.state.dirX*dpad.state.magnitude - 5, screen->h/2 - screen->h/2*dpad.state.dirY*dpad.state.magnitude - 5, 10, 10};
			SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 255, 150, 0));
		}
		SDL_Flip(screen);
		SDL_Delay(1);
    }
    
    SDL_Quit();
    return 0;
}
